﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.ComponentModel;
using System.Windows.Input;
using Infrastructure.ViewModels;

namespace Infrastructure.Controls
{
    /// <summary>
    /// Interaction logic for RtfEditor.xaml
    /// </summary>
    public partial class RtfEditor : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// Document range for the RTF control.
        /// </summary>
        public TextRange DocumentRange;

        /// <summary>
        /// Occurs when [document changed].
        /// </summary>
        public event EventHandler<EventArgs> DocumentChanged;

        public CatalogItemViewModel Model { get; set; }

        public Visibility IsSaveVisible
        {
            get
            {
                return SaveComment.Visibility;
            }
            set
            {
                SaveComment.Visibility = value;
            }
        }

        /// <summary>
        /// Called when [document changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnDocumentChanged(object sender, TextChangedEventArgs e)
        {
            if(DocumentChanged != null)
            {
                DocumentChanged(sender, e);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RtfEditor"/> class.
        /// </summary>
        public RtfEditor()
        {
            InitializeComponent();

            DocumentRange = new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd);

            documentContent.TextChanged += OnDocumentChanged;
        }


        /// <summary>
        /// Gets or sets the text of the document.
        /// </summary>
        /// <value>The text of the document.</value>
        public string Document
        {
            get
            {
                DocumentRange = new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd);
                return DocumentRange.Text;
            }
            set
            {
                documentContent.Document.Blocks.Clear();
                DocumentRange = new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd);
                DocumentRange.Text = value;
                OnPropertyChanged("Document");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the toolbar is visible.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the toolbar is visible; otherwise, <c>false</c>.
        /// </value>
        public bool IsToolbarVisible { 
            get
            {
                return toolBar1.IsVisible;
            } 
            set
            {
                if (toolBar1.IsVisible != value)
                {
                    switch (value)
                    {
                        case true:
                            toolBar1.Visibility = Visibility.Visible;
                            break;
                        case false:
                            toolBar1.Visibility = Visibility.Collapsed;
                            break;

                    }
                    OnPropertyChanged("IsToolbarVisible");
                }
            }
        }


        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="name">The name.</param>
        protected virtual void OnPropertyChanged(string name)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }



        /// <summary>
        /// Handles the Click event of the SaveCommentButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SaveCommentButton_Click(object sender, RoutedEventArgs e)
        {
            Model.Comment = Document;
            Model.SaveCommand.Execute(null);
        }
    }
}